package com.diabeco.ui.common;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

public class DatePickerFragment extends DialogFragment implements
		DatePickerDialog.OnDateSetListener {
	public static final String INITIAL_DAY_OF_MONTH = DatePickerFragment.class
			.getName() + "initialDay";
	public static final String INITIAL_MONTH = DatePickerFragment.class
			.getName() + "initialMonth";
	public static final String INITIAL_YEAR = DatePickerFragment.class
			.getName() + "initialYear";

	private DatePickerDialog.OnDateSetListener activity;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		int year = getArguments().getInt(INITIAL_YEAR);
		int month = getArguments().getInt(INITIAL_MONTH);
		int day = getArguments().getInt(INITIAL_DAY_OF_MONTH);

		return new DatePickerDialog(getActivity(), this, year, month, day);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		try {
			this.activity = (OnDateSetListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnDateSetListener");
		}
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		this.activity.onDateSet(view, year, monthOfYear, dayOfMonth);
	}

}